<script setup>
import BackLink from "@/components/BackLink/index.vue"
import {useRoute, useRouter} from "vue-router";
import {getActualOutPutListApi, getCertificateListApi, getStuDetailsApi} from "@/api/school.js";
import UserBaseInfo from "@/components/UserBaseInfo/index.vue";
import FilePreView from "@/components/FilePreView/index.vue";
import {isExternal} from "@/utils/validate.js";
import {formatDate} from "@/utils/index.js";

const router = useRouter()
const route = useRoute()

const state = reactive({
  studentInfo: {},
  tableData: [],
  queryParams: {
    pageNum: 1,
    pageSize: 8,
    stuId: '',
  },
  total: 0,
})

const showDia1 = ref(false)

const handleBack = () => {
  router.go(-1)
}

const getDataList = async () => {
  const res = await getCertificateListApi(state.queryParams)
  if (res.code == 200) {
    state.tableData = res.rows
    state.total = res.total
  }
}

const getStuDetails = async () => {
  const res = await getStuDetailsApi({id: state.queryParams.stuId})
  if (res.code == 200) {
    state.studentInfo = res.data
  }
}


const handleDetails = (item) => {
  state.curItem = item
  if (item.acFileType === 'document') {
    const src = import.meta.env.VITE_APP_BASE_API + item.acFile
    window.open(src)
  } else {
    showDia1.value = true
  }
}

onMounted(() => {
  const id = route.query.id
  state.queryParams.stuId = id
  getDataList()
  getStuDetails()
})

</script>

<template>
  <div class="outBox">
    <el-card>
      <BackLink/>
      <div class="form-box">
        <UserBaseInfo :student-info="state.studentInfo"/>
        <div class="form-textArea">
          <div class="form-item">
            <div class="form-name">证书展示</div>
            <div class="text-items">
              <div class="text-item" v-for="item in state.tableData">
                <div class="tit">{{ item.acTitle }}</div>
                <div class="bottom">
                  <div class="time">{{ item.createTime }}</div>
                  <div class="check">
                    <el-button type="primary" link @click="handleDetails(item)">查看详情</el-button>
                  </div>
                </div>
              </div>

            </div>
          </div>
          <div class="pageBox">
            <pagination
                v-show="state.total > 0"
                :total="state.total"
                v-model:page="state.queryParams.pageNum"
                v-model:limit="state.queryParams.pageSize"
                @pagination="getDataList()"
                layout="'total, prev, pager, next, jumper'"
            />
          </div>
        </div>
        <div class="center-btns">
          <el-button @click="handleBack" type="primary">返回</el-button>
        </div>
      </div>
    </el-card>
    <el-dialog title="查看详情" v-model="showDia1" width="800" center>
      <el-form>
        <el-form-item label="证书名称：">
          <p class="form-text">{{state.curItem.acTitle}}</p>
        </el-form-item>
        <el-form-item label="创建时间：">
          <p class="form-text">{{formatDate(state.curItem.createTime)}}</p>
        </el-form-item>
        <el-form-item label="证书材料：">
        </el-form-item>
      </el-form>
      <div class="dia-cont">
        <FilePreView :url="state.curItem.acFile" width="500px"  :fileType="state.curItem.acFileType"/>
      </div>

      <template #footer>
        <div class="dia-footer">
          <el-button @click="showDia1 = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.dia-cont,.dia-footer{
  display: flex;
  justify-content: center;
}
.text-items {
  padding: 20px;
  display: grid;
  justify-content: space-between;
  grid-template-columns: repeat(2, 550px);

  .text-item {
    margin-bottom: 20px;
  }

  .tit {
    font-size: 22px;
    margin-bottom: 5px;
  }

  .bottom {
    display: flex;
    justify-content: space-between;

    .time {
      color: #827e7d;
    }
  }
}

.center-btns {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.form-textArea {
  border: 1px solid #e5e5e5;
  min-height: 400px;

  :deep(.el-textarea) {
    --el-input-border-color: #fff !important;
    --el-input-focus-border: #fff !important;
    --el-input-hover-border-color: #fff !important;
    --el-input-focus-border-color: #fff !important;

  }
}

.form-box {
  .form-name {
    background: #f1f7ff;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100px;
    font-weight: bold;
    border-right: 1px solid #e5e5e5;
  }

  .form-line {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    border: 1px solid #e5e5e5;
    margin-bottom: 20px;

    .form-item {
      display: flex;
      align-items: center;

      .form-cont {
        width: calc(100% - 100px);

        :deep(.el-input) {
          --el-input-border-color: #fff !important;
          --el-input-focus-border: #fff !important;
          --el-input-focus-border-color: #fff !important;
        }
      }
    }
  }
}
</style>
